package com.hornsun.repository;

import com.google.common.base.Preconditions;
import com.hornsun.util.StrUtil;
import com.hornsun.domain.dbo.HealthIndex;
import com.hornsun.domain.dbo.HealthIndexExample;
import com.hornsun.repository.mapper.HealthIndexMapper;
import com.hornsun.util.LittleUtil;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by 编译中... on 2018/4/4.
 */
@Repository
public class HealthIndexRepository {

    @Resource
    private HealthIndexMapper healthIndexMapper;

    @Resource
    private LittleUtil littleUtil;

    /**
     * 获取当天健康指标记录
     * @param guid
     * @return
     */
    public HealthIndex getTodayHealthIndex(String guid){
        if (guid==null||guid.isEmpty()){
            return null;
        }
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = sdf.format(date)+"%";
        return healthIndexMapper.selectTodayHelthIndex(timeStr,guid);
    }

    /**
     * 新增健康指标记录
     * @param healthIndex
     */
    public void addHealthIndex(HealthIndex healthIndex){
        int count = healthIndexMapper.insertSelective(healthIndex);
        if (count!=1){
            throw new IllegalArgumentException("add healthIndex failed by healthIndex:"+healthIndex);
        }
    }

    /**
     * 检测当天是否存在健康指标记录
     * @param memberGuid
     * @return
     */
    public boolean checkTodayExist(String memberGuid){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = sdf.format(date)+"%";
       return healthIndexMapper.queryTodayCount(timeStr,memberGuid)==0;
    }

    /**
     * 通过会员guid获取近期健康指标
     * @param memberGuid
     * @return
     */
    public List<HealthIndex> getNearHealthIndex(String memberGuid){
        Preconditions.checkNotNull(memberGuid);
        HealthIndexExample healthIndexExample = new HealthIndexExample();
        healthIndexExample.setOrderByClause(StrUtil.toDesc("time"));
        HealthIndexExample.Criteria criteria = healthIndexExample.createCriteria();
        criteria.andMemberGuidEqualTo(memberGuid);
        return healthIndexMapper.selectByExampleWithRowbounds(healthIndexExample, littleUtil.littleRowBounds(1));
    }

    /**
     * 修改当天健康指标
     * @param healthIndex
     */
    public void updateTodayHealthIndex(HealthIndex healthIndex,String memberGuid){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        HealthIndexExample healthIndexExample = new HealthIndexExample();
        HealthIndexExample.Criteria criteria = healthIndexExample.createCriteria();
        criteria.andTimeGreaterThan(zero).andMemberGuidEqualTo(memberGuid);
        healthIndexMapper.updateByExampleSelective(healthIndex,healthIndexExample);
    }

}
